package com.jyw.business.ins.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.jyw.business.ast.domain.dto.web.AstItemPageDto;
import com.jyw.business.ast.domain.vo.web.AstItemPageVo;
import com.jyw.business.ast.mapper.AstInfoMapper;
import com.jyw.business.ins.domain.dto.web.ItemListDto;
import com.jyw.business.ins.domain.dto.web.UnitItemPageDto;
import com.jyw.business.ins.domain.vo.web.InsPlanItemVO;
import com.jyw.business.ins.domain.vo.web.ItemListVo;
import com.jyw.business.ins.domain.vo.web.UnitItemPageVo;
import com.jyw.business.ins.mapper.InsUnitMapper;
import com.jyw.system.utils.ConversionUtil;
import org.springframework.stereotype.Service;
import com.jyw.business.ins.mapper.InsConfigItemMapper;
import com.jyw.business.ins.domain.InsConfigItem;
import com.jyw.business.ins.service.IInsConfigItemService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;

/**
 * 巡检配置项Service业务层处理
 *
 * @author system
 * @date 2023-09-20
 */
@Service
public class InsConfigItemServiceImpl extends ServiceImpl<InsConfigItemMapper, InsConfigItem> implements IInsConfigItemService {
    @Resource
    private InsConfigItemMapper insConfigItemMapper;

    @Resource
    private AstInfoMapper astInfoMapper;

    @Resource
    private InsUnitMapper unitMapper;

    /**
     * 查询巡检配置项
     *
     * @param id 巡检配置项主键
     * @return 巡检配置项
     */
    @Override
    public InsConfigItem selectInsConfigItemById(Long id) {
        return insConfigItemMapper.selectInsConfigItemById(id);
    }

    /**
     * 查询巡检配置项列表
     *
     * @param insConfigItem 巡检配置项
     * @return 巡检配置项
     */
    @Override
    public List<InsConfigItem> selectInsConfigItemList(InsConfigItem insConfigItem) {
        return insConfigItemMapper.selectInsConfigItemList(insConfigItem);
    }

    @Override
    public List<InsPlanItemVO> getItemList(Long parentId, Integer parentType) {
        QueryWrapper<InsConfigItem> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(InsConfigItem::getParentId, parentId);
        queryWrapper.lambda().eq(InsConfigItem::getParentType, parentType.intValue());
        List<InsConfigItem> items = this.list(queryWrapper);
        if (CollectionUtil.isNotEmpty(items)) {
            List<InsPlanItemVO> itemVOS = new ArrayList<>();
            items.forEach(item -> {
                InsPlanItemVO itemVO = new InsPlanItemVO();
                itemVO.setConfigItemId(item.getId());
                itemVO.setItemName(item.getItemName());
                itemVO.setItemType(item.getItemType());
                itemVOS.add(itemVO);
            });
            List<InsPlanItemVO> insConfigItems = ConversionUtil.conversionList(itemVOS, InsPlanItemVO.class);
            return insConfigItems;
        }
        return null;
    }

    /**
     * 新增巡检配置项
     *
     * @param insConfigItem 巡检配置项
     * @return 结果
     */
    @Override
    public int insertInsConfigItem(InsConfigItem insConfigItem) {
        return insConfigItemMapper.insertInsConfigItem(insConfigItem);
    }

    /**
     * 修改巡检配置项
     *
     * @param insConfigItem 巡检配置项
     * @return 结果
     */
    @Override
    public int updateInsConfigItem(InsConfigItem insConfigItem) {
        return insConfigItemMapper.updateInsConfigItem(insConfigItem);
    }

    /**
     * 批量删除巡检配置项
     *
     * @param ids 需要删除的巡检配置项主键
     * @return 结果
     */
    @Override
    public int deleteInsConfigItemByIds(Long[] ids) {
        return insConfigItemMapper.deleteInsConfigItemByIds(ids);
    }

    /**
     * 删除巡检配置项信息
     *
     * @param id 巡检配置项主键
     * @return 结果
     */
    @Override
    public int deleteInsConfigItemById(Long id) {
        return insConfigItemMapper.deleteInsConfigItemById(id);
    }

    @Override
    public List<ItemListVo> list(ItemListDto dto) {
        LambdaQueryChainWrapper<InsConfigItem> lambdaQuery = lambdaQuery()
                .eq(InsConfigItem::getParentId, dto.getParentId())
                .eq(InsConfigItem::getParentType, dto.getParentType());
        //巡检项名称
        if (!ObjectUtils.isEmpty(dto.getItemName())) {
            lambdaQuery.like(InsConfigItem::getItemName, dto.getItemName());
        }
        //是否启用
        if (!ObjectUtils.isEmpty(dto.getIsEnable())) {
            lambdaQuery.eq(InsConfigItem::getIsEnable, dto.getIsEnable());
        }
        lambdaQuery.orderByAsc(InsConfigItem::getSort);
        //查询出巡检项
        List<InsConfigItem> list = lambdaQuery.list();
        //转换成列表数据
        List<ItemListVo> collect = list.stream().map(ItemListVo::new).collect(Collectors.toList());
        return ConversionUtil.conversionList(collect, ItemListVo.class);
    }

    @Override
    public List<AstItemPageVo> astList(AstItemPageDto dto) {
        List<AstItemPageVo> list = astInfoMapper.astItemList(dto);
        return ConversionUtil.conversionList(list, AstItemPageVo.class);
    }

    @Override
    public List<UnitItemPageVo> unitList(UnitItemPageDto dto) {
        List<UnitItemPageVo> list = unitMapper.unitItemList(dto);
        return ConversionUtil.conversionList(list, UnitItemPageVo.class);
    }
}
